package org.fintan.make.controller;

import javax.websocket.server.PathParam;

import org.fintan.make.entity.User;
import org.fintan.make.master.ExecuteResult;
import org.fintan.make.params.UserParam;
import org.fintan.make.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import springfox.documentation.annotations.ApiIgnore;

@Api(description = "用户相关接口文档")
@RestController
@RequestMapping(value = "user")
public class UserController {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserService userService;
	@ApiOperation(value="注册用户",notes="用户增加",produces = "application/json")
	@ApiImplicitParam(name = "user", value = "用户", dataType="User",paramType="body")
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public ExecuteResult<?> registerUser(@ModelAttribute @RequestBody User user){
		//ModelAttribute 是Spring mvc的注解，这里Swagger可以解析这个注解，获得User的属性描述
		if(StringUtils.isEmpty(user.getUserName())){
			log.info("用户名为空");
			return ExecuteResult.fail("用户名为空");
		}
		if(StringUtils.isEmpty(user.getPassword())){
			log.info("用户密码为空");
			return ExecuteResult.fail("用户密码为空");
		}
		if(!userService.insert(user)){
			return ExecuteResult.fail("用户名已存在");
		}
		
		return ExecuteResult.ok("注册成功");
	}
	@ApiOperation(value="获取用户信息",notes="查询用户",produces = "application/json")
	@RequestMapping(value = "queryUser" , method = RequestMethod.GET)
	public ExecuteResult<?> criteriaQuery(/*@RequestBody UserParam param*/){
		return ExecuteResult.ok(userService.selectUserByCondition(null));
	}
	//produces = "application/json"  可以将实体类显示出来
	@ApiOperation(value="修改用户信息",notes="修改用户",produces = "application/json")
	@ApiImplicitParam(name = "user", value = "用户", dataType="User",paramType="body")
	@RequestMapping(value = "updateUser" , method = RequestMethod.POST)
	public ExecuteResult<?> updateUser(@ModelAttribute @RequestBody User user){
		//ModelAttribute 是Spring mvc的注解，这里Swagger可以解析这个注解，获得User的属性描述
		//////*******************2018-2-9日必须加上swagger*****************
		if(user == null){
			log.info("修改用户参数为空");
			return ExecuteResult.fail("修改用户参数为空");
		}
		return userService.updateUser(user);
	}
	@ApiIgnore  //表示这个方法或者类被忽略 
	@RequestMapping(value = "deleteUser/{id}" , method = RequestMethod.GET)
	public ExecuteResult<?> deleteUser(@ApiParam(name="id",value="用户id",required=true) @PathVariable("id") Integer id){
		return userService.delectUser(id);
	}
	
	
	@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息",produces = "application/json")
	// ApiResponses 增加返回结果的描述
	@ApiResponses(value = {@ApiResponse(code = 405,message = "Invalid input",response = Integer.class)})// (1)
	@ApiImplicitParam(name = "id",value = "用户ID",dataType = "int",paramType = "path") // (2)
	@RequestMapping(value = "queryUserById/{id}" , method = RequestMethod.GET)
	public ExecuteResult<?> queryUserById(@ApiParam(name="id",value="用户id",required=true) @PathVariable("id") Integer id){
		if(id == null){
			return ExecuteResult.fail("查询用户id为空");
		}
		
		return ExecuteResult.ok(userService.selectUserById(id));
	}
	
}
